package cn.iocoder.yudao.module.resource.service.collect;

import cn.iocoder.yudao.module.resource.dal.dataobject.collect.ResourceCollectDO;
import cn.iocoder.yudao.module.resource.dal.mysql.collect.ResourceCollectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户收藏 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ResourceCollectServiceImpl implements ResourceCollectService {

    @Resource
    private ResourceCollectMapper collectMapper;

    @Override
    public Long createCollect(ResourceCollectDO collectDO) {
        collectMapper.insert(collectDO);
        return collectDO.getId();
    }

    @Override
    public void deleteCollect(Long id) {
        collectMapper.deleteById(id);
    }

    @Override
    public void cancelCollect(Long userId, Long materialId) {
        ResourceCollectDO collect = getCollectByUserIdAndMaterialId(userId, materialId);
        if (collect != null) {
            collectMapper.deleteById(collect.getId());
        }
    }

    @Override
    public ResourceCollectDO getCollect(Long id) {
        return collectMapper.selectById(id);
    }

    @Override
    public ResourceCollectDO getCollectByUserIdAndMaterialId(Long userId, Long materialId) {
        return collectMapper.selectByUserIdAndMaterialId(userId, materialId);
    }

    @Override
    public List<ResourceCollectDO> getCollectListByUserId(Long userId) {
        return collectMapper.selectListByUserId(userId);
    }

    @Override
    public boolean hasCollected(Long userId, Long materialId) {
        return getCollectByUserIdAndMaterialId(userId, materialId) != null;
    }

}
